home *** CD-ROM | disk | FTP | other *** search
/ MACD 5 / MACD 5.bin / workbench / blankery / blanker / source / blankers / flyingtoaster / blank.c next >
C/C++ Source or Header  |  1993-08-15  |  6KB  |  235 lines

  1. #include <exec/types.h>
  2. #include <exec/memory.h>
  3.  
  4. #include <intuition/intuition.h>
  5. #include <intuition/intuitionbase.h>
  6. #include <intuition/screens.h>
  7.  
  8. #include <dos/dos.h>
  9.  
  10. #include <clib/exec_protos.h>
  11. #include <clib/dos_protos.h>
  12. #include <clib/intuition_protos.h>
  13. #include <clib/graphics_protos.h>
  14. #include <clib/alib_protos.h>
  15.  
  16. #include "FlyingToaster.h"
  17. #include "images.h"
  18. #include "/defs.h"
  19.  
  20. #define    IM_WIDTH    64
  21. #define    IM_HEIGHT    64
  22. #define    IM_RADIUS    120
  23. #define    LOOPRATE    10
  24. #define    FACERATE    10
  25. #define    RATE_DEC    5
  26. #define    FT_SPEED    ( RangeRand( 4 ) + 1 )
  27. #define    FT_RATE( x )    ( 2 * ( 4 - ft[x].speed ))
  28. #define    MAX_FT    40
  29.  
  30. struct mPrefObject {
  31.     LONG    Objects;
  32.     LONG    Speed;
  33. };
  34.  
  35. struct    object{
  36.     int    delay;
  37.     int    x,y;
  38.     int    old_x,old_y;
  39.     int    speed;
  40.     int    rate;
  41.     int    phase;
  42.     int    rate_count;
  43.     struct    sequence    *seq;
  44. };
  45.  
  46. extern    struct    mPrefObject    nP;
  47. extern        ULONG        Mode;
  48. extern        UBYTE        *prefData;
  49.     struct    object        *ft;
  50.         USHORT        nr_objects;
  51.     struct    sequence    *seq_list[] = { NULL, &default_seq, &loop_seq, &face_seq };
  52.  
  53. LONG check_collision( LONG i, LONG x, LONG y )
  54. {
  55.     LONG    j, jx, jy;
  56.  
  57.     x += IM_WIDTH / 2;
  58.     y += IM_HEIGHT / 2;
  59.  
  60.     for( j = 0; j < nr_objects; j++ ) {
  61.         if( j != i && !ft[j].delay ) {
  62.             jx = ft[j].x + IM_WIDTH / 2;
  63.             jy = ft[j].y + IM_HEIGHT / 2;
  64.  
  65.             if((( jx - x )*( jx - x )+( jy - y )*( jy - y )) <= 4 * IM_RADIUS * IM_RADIUS ) return( j );
  66.         }
  67.     }
  68.     return( -1 );
  69. }
  70.  
  71. VOID calc_new_pos( LONG i )
  72. {
  73.     LONG    x, y, c;
  74.  
  75.     x = ft[i].x - ft[i].speed;
  76.     y = ft[i].y + 2;
  77.  
  78.     ft[i].old_x = ft[i].x;
  79.     ft[i].old_y = ft[i].y;
  80.  
  81.     if(( c = check_collision( i, x, y )) != -1 ) {
  82.         x = ft[i].x;
  83.         y += 2;
  84.         if(( c = check_collision( i, x, y )) != -1 ) {
  85.             y = ft[i].y - 4;
  86.             if(( c = check_collision( i, x, y )) != -1 ) return;
  87.         }
  88.     }
  89.     ft[i].x = x;
  90.     ft[i].y = y;
  91. }
  92.  
  93. BOOL find_launch_pos( LONG i, LONG wid, LONG hei )
  94. {
  95.     BOOL    colides;
  96.     LONG    j, x, y, ix, iy, jx, jy;
  97.  
  98.     x = wid;
  99.     y = hei;
  100.  
  101.     FOREVER {
  102.         if( x <= 2*IM_WIDTH && y <= -IM_HEIGHT ) return(FALSE);
  103.  
  104.         ix = x + IM_WIDTH / 2;
  105.         iy = y + IM_HEIGHT / 2;
  106.         colides = FALSE;
  107.  
  108.         for( j = 0; j < nr_objects && !colides; j++ ) {
  109.             if( i != j ) {
  110.                 jx = ft[j].x + IM_WIDTH / 2;
  111.                 jy = ft[j].y + IM_HEIGHT / 2;
  112.  
  113.                 if((( jx - ix )*( jx - ix )+( jy - iy )*( jy - iy )) <=
  114.                     (4 * IM_RADIUS * IM_RADIUS)) colides = TRUE;
  115.             }
  116.         }
  117.  
  118.         if( !colides ) {
  119.             ft[i].x = ft[i].old_x = x;
  120.             ft[i].y = ft[i].old_y = y;
  121.             return( TRUE );
  122.         }
  123.         if( y > - IM_HEIGHT ) y -= IM_HEIGHT;
  124.         else x -= IM_WIDTH;
  125.     }
  126. }
  127.  
  128. VOID goto_next_img( struct object *obj, struct sequence *next_seq )
  129. {
  130.     obj->phase++;
  131.     if( obj->phase >= obj->seq->img_count ) {
  132.         obj->phase = 0;
  133.         if( !next_seq ) {
  134.             obj->seq = obj->seq->next_sequence;
  135.             if( !RangeRand( LOOPRATE )) {
  136.                 obj->rate = RATE_DEC - 1;
  137.                 obj->seq = &loop_seq;
  138.             } else if( !RangeRand( FACERATE )) {
  139.                 obj->rate = RATE_DEC - 1;
  140.                 obj->seq = &face_seq;
  141.             }
  142.         } else obj->seq = next_seq;
  143.     }
  144. }
  145.  
  146. VOID blank( VOID )
  147. {
  148.     struct    mPrefObject    *mP;
  149.     struct    Screen        *FTScr;
  150.     struct    Window        *win;
  151.     struct    RastPort    *Rast;
  152.     struct    ColorSpec    C[] = { 0, 0x00, 0x00, 0x00, 1, 0x0F, 0x0F, 0x0F, ~0, 0x00, 0x00, 0x00 };
  153.         UWORD        P[] = { 65535 };
  154.         LONG        delay_rate = 2, i;
  155.  
  156.     if( FlyingToasterWnd ) mP = &nP;
  157.     else mP = ( struct mPrefObject * )prefData;
  158.  
  159.     nr_objects = mP->Objects;
  160.     delay_rate = mP->Speed;
  161.  
  162.     if(!( ft = AllocVec( sizeof( struct object ) * nr_objects, MEMF_CLEAR ))) return;
  163.  
  164.     if( FTScr = OpenScreenTags( NULL, SA_DisplayID, Mode, SA_Depth, 1, SA_Overscan, OSCAN_STANDARD, SA_Colors,
  165.         (LONG)C, SA_Type, CUSTOMSCREEN, SA_Pens, (LONG)P, SA_Quiet, TRUE, SA_Behind, TRUE, TAG_DONE )) {
  166.  
  167.         SetRGB4( &( FTScr->ViewPort ), 0, 0, 0, 0 );
  168.         SetRGB4( &( FTScr->ViewPort ), 1, 0x0F, 0x0F, 0x0F );
  169.         setCopperList( FTScr->Height, 1L , &( FTScr->ViewPort ));
  170.  
  171.         if( win = OpenWindowTags( NULL, WA_Width, FTScr->Width, WA_Height, FTScr->Height, WA_IDCMP, 0L,
  172.             WA_Flags, WFLG_SIMPLE_REFRESH|WFLG_BORDERLESS, WA_CustomScreen, FTScr, TAG_DONE )) {
  173.  
  174.             Rast = win->RPort;
  175.             SetRast( Rast, 0 );
  176.             SetAPen( Rast, 0 );
  177.  
  178.             for( i = 0; i < nr_objects; i++ ) {
  179.                 if( !find_launch_pos( i, win->Width, win->Height )) ft[i].delay = 30;
  180.                 else ft[i].delay = RangeRand( 50 );
  181.  
  182.                 ft[i].phase = RangeRand( IMAGEMAX );
  183.                 ft[i].seq = &default_seq;
  184.                 ft[i].speed = FT_SPEED;
  185.                 ft[i].rate = ft[i].rate_count = FT_RATE( i );
  186.             }
  187.  
  188.             BlankMousePointer();
  189.             ScreenToFront( FTScr );
  190.  
  191.             while(!( SetSignal( 0L, 0L ) & SIGBREAKF_CTRL_C )) {
  192.                 for( i = 0; i < nr_objects; i++ ) {
  193.                     if( !ft[i].delay ) {
  194.                         calc_new_pos( i );
  195.                         ft[i].rate_count -= RATE_DEC;
  196.  
  197.                         if( ft[i].rate_count < 0 ) {
  198.                             ft[i].rate_count = ft[i].rate;
  199.                             goto_next_img( &ft[i], NULL );
  200.                         }
  201.  
  202.                         if(( ft[i].old_x != ft[i].x || ft[i].old_y != ft[i].y )) {
  203.                             if( ft[i].y > ft[i].old_y ) EraseRect( Rast, ft[i].old_x,
  204.                                 ft[i].old_y, ft[i].old_x + IM_WIDTH,
  205.                                 ft[i].y + 1 );
  206.                             if( ft[i].x < ft[i].old_x ) EraseRect( Rast,
  207.                                 ft[i].x + IM_WIDTH - 1, ft[i].y,
  208.                                 ft[i].old_x + IM_WIDTH, ft[i].old_y + IM_HEIGHT );
  209.                         }
  210.  
  211.                         DrawImage( Rast, ft[i].seq->img[ft[i].phase], ft[i].x, ft[i].y );
  212.  
  213.                         if( ft[i].x < -( IM_WIDTH + 1 ) || ft[i].y > win->Height )
  214.                             ft[i].delay = RangeRand( 50 );
  215.                     } else {
  216.                         ft[i].delay--;
  217.                         if( !ft[i].delay ) {
  218.                             if( find_launch_pos( i, win->Width, win->Height )) {
  219.                                 ft[i].speed = FT_SPEED;
  220.                                 ft[i].rate = ft[i].rate_count = FT_RATE( i );
  221.                             } else ft[i].delay = 30;
  222.                         }
  223.                     }
  224.                 }
  225.                 Delay( 3 - delay_rate );
  226.             }
  227.             SetSignal( 0L, SIGBREAKF_CTRL_C );
  228.             UnblankMousePointer();
  229.             CloseWindow( win );
  230.         }
  231.         CloseScreen( FTScr );
  232.     }
  233.     FreeVec( ft );
  234. }
  235.